<?php
session_start();
// 引入数据库连接代码
include 'conn.php';
include 'function.php';

// 处理修改信息的函数
function updateUserInfo($conn, $user_id, $newEmail, $newPhone)
{
    // 更新数据库中的邮箱和电话信息
    $updateInfoSQL = "UPDATE users SET email = '$newEmail', phone = '$newPhone' WHERE user_id = $user_id";
    
    return mysqli_query($conn, $updateInfoSQL);
}

// 处理修改信息 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 获取用户ID
    $user_id = $_SESSION['user_id']; // 假设用户ID存储在 session 中

    // 获取新邮箱和新电话
    $newEmail = mysqli_real_escape_string($conn, $_POST['new_email']);
    $newPhone = mysqli_real_escape_string($conn, $_POST['new_phone']);

    // 调用函数更新用户信息
    if (updateUserInfo($conn, $user_id, $newEmail, $newPhone)) {
        echo '<script>alert("信息修改成功"); window.location.href = "userinfo.php";</script>';
    } else {
        echo "信息更新失败：" . mysqli_error($conn);
    }
}

// 关闭数据库连接
mysqli_close($conn);
?>
